package com.globallogic;

import java.util.NoSuchElementException;

public class OurList<E> implements List<E> {

	private Object[] objectsArrayList;

	private int size = 0;

	private static final int INITIAL_SIZE = 10;
	private static final int INCREMENT_SIZE = 5;

	public OurList() {
		objectsArrayList = new Object[INITIAL_SIZE];
	}

	@Override
	public int size() {
		return size;
	}

	@Override
	public boolean contains(Object innerObject) {
		if (innerObject == null) {
			throw new NullPointerException();
		}
		for (int i = 0; i < objectsArrayList.length; i++) {
			if (innerObject.equals(objectsArrayList[i])) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean add(E incomingObject) {
		if (incomingObject == null) {
			throw new NullPointerException();
		}
		if (size == objectsArrayList.length) {
			Object[] biggestList = new Object[objectsArrayList.length
					+ INCREMENT_SIZE];
			System.arraycopy(objectsArrayList, 0, biggestList, 0,
					objectsArrayList.length);
			objectsArrayList = biggestList;
		}
		objectsArrayList[size++] = incomingObject;
		return true;
	}

	@Override
	public boolean remove(Object incomingObject) {
		if (incomingObject == null) {
			throw new NullPointerException();
		}
		int index = 0;

		for (int i = 0; i < size; i++) {
			if (incomingObject.equals(objectsArrayList[i])) {
				index = i;
			}
		}
		remove(index);
		return true;
	}

	@Override
	public void clear() {
		size = 0;
		Object[] emptyList = new Object[INITIAL_SIZE];
		objectsArrayList = emptyList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public E get(int index) {
		if (index < 0 || index > size) {
			throw new IndexOutOfBoundsException();
		}
		return (E) objectsArrayList[index];
	}

	@SuppressWarnings("unchecked")
	@Override
	public E remove(int index) {
		if (index < 0 || index + 1 > size) {
			throw new IndexOutOfBoundsException();
		}
		E removeObj = (E) objectsArrayList[index];
		int numbToMove = size - index - 1;
		System.arraycopy(objectsArrayList, index + 1, objectsArrayList, index,
				numbToMove);

		objectsArrayList[--size] = null;
		return removeObj;
	}

	public void printMe() {
		for (int i = 0; i < objectsArrayList.length; i++) {
			System.out.println(objectsArrayList[i]);
		}

	}

	public Iterator<E> iterator() {
		return new OurIterator();
	}

	private class OurIterator implements Iterator<E> {

		private int currentElement = -1;

		@Override
		public boolean hasNext() {
			while (objectsArrayList[currentElement + 1] != null) {// || size !=
																	// objectsArrayList.length
				return true;
			}
			return false;
		}

		@SuppressWarnings("unchecked")
		@Override
		public E next() {
			if(objectsArrayList[++currentElement]==null){
			throw new NoSuchElementException();
		}
			return (E) objectsArrayList[currentElement];
		}

		@Override
		public void remove() {
			if (currentElement != -1)
				objectsArrayList[currentElement] = null;
			size--;
		}
	}
}
